package com.winterwar.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

import com.winterwar.base.RegisteredEvent;
import com.winterwar.service.ConventionManager;
import com.winterwar.service.RegisteredEventManager;

public class RegisteredEventAction extends DispatchAction{
	private static Log log = LogFactory.getLog(RegisteredEventAction.class);
	private RegisteredEventManager mgr;
	private ConventionManager conMgr;
	
	public void setConventionManager(ConventionManager mgr){
		this.conMgr = mgr;
	}
	
	public void setManager(RegisteredEventManager mgr){
		this.mgr = mgr;
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		if(log.isDebugEnabled()){
			log.debug("Starting delete...");
		}
		mgr.remove(request.getParameter("registeredEvent.registeredEventID"));
		ActionMessages messages = new ActionMessages();
		messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("registeredEvent.deleted"));
		saveMessages(request, messages);
		request.setAttribute("regEvents", mgr.getByRegistration(request.getParameter("regID")));
		return mapping.findForward("success");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		if(log.isDebugEnabled()){
			log.debug("Starting save...");
		}
		DynaActionForm registeredEventForm = (DynaActionForm) form;
		RegisteredEvent event = (RegisteredEvent) registeredEventForm.get("registeredEvent");
		mgr.save(event);
				
		ActionMessages messages = new ActionMessages();
		messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("registeredEvent.saved"));
		saveMessages(request, messages);
		
		request.setAttribute("regEvents", mgr.getByRegistration(event.getRegID().toString()));
		
		return mapping.findForward("success");
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form,
					HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		if(log.isDebugEnabled()){
			log.debug("Starting edit...");
		}
		DynaActionForm registeredEventForm = (DynaActionForm) form;
		String registeredEventID = request.getParameter("registeredEventID");
		
		if(registeredEventID != null){
			RegisteredEvent registeredEvent = mgr.get(registeredEventID);
			if(registeredEvent == null){
				ActionMessages errors = new ActionMessages();
				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("registeredEvent.missing"));
				saveErrors(request, errors);
				
				return mapping.findForward("list");
			}
			registeredEventForm.set("registeredEvent", registeredEvent);
		}
		return mapping.findForward("edit");
	}
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
						HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		if(log.isDebugEnabled()){
			log.debug("Starting list...");
		}
		request.setAttribute("registeredEvents", mgr.getAll());
		
		return mapping.findForward("list");
	}
}
